package com.ybt.communicate.core.endpoint;

import com.ruleengine.context.IContextProperty;
import com.ruleengine.context.IWorkflowConext;
import com.ruleengine.endpoint.IEndpoint;
import com.ruleengine.endpoint.callback.IEndpointCallBack;
import com.ruleengine.endpoint.data.IEndpointData;
import com.ruleengine.endpoint.exception.EndpointException;
import com.ybt.communicate.core.IConnection;
import com.ybt.communicate.core.callback.IConnectionCallback;
import com.ybt.communicate.core.exception.ProtocolException;
import com.ybt.context.IYBTContextConstants;

/**
 * 银保通通信服务的endpoint适配器.
 * 
 * @author wuxiao
 * 
 * Created on Jul 22, 2012
 */
public class CommEndpoint implements IEndpoint {

    /**
     * 本地服务名称
     */
    private String target;

    /**
     * 通信连接对象
     */
    private IConnection connection;

    public IEndpointData send(IWorkflowConext context, IEndpointData data)
            throws EndpointException {
        // TODO Auto-generated method stub
        try {
            Object result = connection.send(context, data);
            // 将原始报文放到Context中
            IContextProperty p = context
                    .getContextProperty(IYBTContextConstants.ORIGINAL_DATA);
            p.setValue(result);
            
            return data;
        } catch (ProtocolException e) {
            // TODO Auto-generated catch block
            throw new EndpointException(e);
        }
    }

    public void sendNonBlocking(final IWorkflowConext context,
            IEndpointData data, final IEndpointCallBack callback)
            throws EndpointException {
        try {
            connection.sendNonBlocking(context, data,
                    new IConnectionCallback() {
                        IWorkflowConext oriContext = context;

                        public void onComplete(final Object data1) {
                            // 异步调用协议返回成功
                            // 将原始报文放到Context中
                            IContextProperty p = oriContext
                                    .getContextProperty(IYBTContextConstants.ORIGINAL_DATA);
                            p.setValue(data1);
                            
                            //调用回调方法
                            callback.onComplete(data1);

                        }

                        public void onError(final Exception cause) {
                            // 异步调用协议返回失败
                            callback.onError(cause);
                        }

                    });
        } catch (ProtocolException e) {
            // TODO Auto-generated catch block
            throw new EndpointException(e);
        }
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public IConnection getConnection() {
        return connection;
    }

    public void setConnection(IConnection connection) {
        this.connection = connection;
    }

}
